package com.info.zhiduoduo.common.configs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * 配置Swagger2 和 全局string转Date
 */
@Configuration
@EnableOpenApi
@EnableConfigurationProperties(UploadPathConfig.class)
public class SwaggerConfig extends WebMvcConfigurationSupport {

	@Value("${swagger.enable}")
	boolean enable;
	@Autowired
	UploadPathConfig uploadPathConfig;

	@Bean
	public Docket productApi() {
		return new Docket(DocumentationType.OAS_30).select()
				// 扫描的包路径
				.apis(RequestHandlerSelectors.basePackage("com.info.zhiduoduo"))
				// 定义要生成文档的Api的url路径规则
				.paths(PathSelectors.any()).build()
				// 设置swagger-ui.html页面上的一些元素信息。
				.apiInfo(metaData())
				.enable(enable);
	}

	private ApiInfo metaData() {
		return new ApiInfoBuilder()
				// 标题
				.title("塾人接口文档")
				// 描述
				.description("测试接口文档")
				// 文档版本
				.version("1.0.0")
				// .license("Apache License Version 2.0")
				// .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
				.build();
	}

	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		//addResourceHandlers方法添加了两个资源处理程序，这段代码的主要作用是对Swagger UI的支持。
		registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/META-INF/resources/")
				.addResourceLocations("classpath:/resources/")
				.addResourceLocations("classpath:/static/")
				.addResourceLocations("classpath:/public/")
				.addResourceLocations("classpath:/")
				.addResourceLocations("file:" + uploadPathConfig.getUploadpath());
	}
}
